#include "CardUI.h"

CardUI::CardUI() : IDialog(), m_selected(false), m_data(0)
{
    RegDialogCtrl("bg", m_click);
    RegDialogCtrl("flag_bg", flag_bg);//背面
    RegDialogCtrl("flag_num", flag_num);//区分
    RegDialogCtrl("flag_color", flag_color);//花色
    RegDialogCtrl("flag_color_big", flag_color_big);//花色
    RegDialogCtrl("flag_king", flag_king);//大小王
    RegDialogCtrl("flag_dz", flag_dz);//地主标志
    RegDialogCtrl("txt", num);
}

CardUI::~CardUI(){
}

void CardUI::onEnter() {
    IDialog::onEnter();
}

void CardUI::onExit() {
    IDialog::onExit();
}

bool CardUI::isSelected() const {
    return m_selected;
}
void CardUI::setSelected(bool state) {
    m_selected = state;
}

void CardUI::showFlag(bool show) {
    this->flag_dz->setVisible(show);
}

void CardUI::onUILoaded() {
    this->flag_bg->setVisible(false);
    this->flag_dz->setVisible(false);
    this->flag_king->setVisible(false);
    this->num->setVisible(false);
    this->m_click->addClickEventListener([this](Ref* sender) {
        m_selected = !m_selected;
        m_ntfMgr->postNotification("ClickCardUI", this);
    });
}

void CardUI::setData(int32_t card, bool show) {
    int32_t num = card % 100;
    int32_t tag = (int32_t)(card / 100);
    flag_bg->setVisible(!show);
    if (num > 12 && tag == 5) {
        // num(13, 14)
        flag_king->setVisible(true);
        flag_num->setVisible(false);
        flag_color->setVisible(false);
        utility::setSpriteFrame(flag_king, (num == 13 ? "poker/king_xw.png" : "poker/king_dw.png"));
        utility::setSpriteFrame(flag_color_big, utility::toString("poker/bigtag_", (num == 13 ? 4 : 5), ".png"));
    } else {
        if (num == 11) num = 0;
        else if (num == 12) num = 1;
        else num += 2;
        flag_king->setVisible(false);
        flag_num->setVisible(true);
        flag_color->setVisible(true);
        // num(0 - 12)  : 3, 4,  5,  6,  7,  8,  9, 10,  J,  Q,  K,  A,  2
        // tag(1,2,3,4) : 黑,红,梅,方
        if (tag == 1 || tag == 3) {
            // 黑桃 or 梅花
            utility::setSpriteFrame(flag_num, utility::toString("poker/black_", num, ".png"));
            utility::setSpriteFrame(flag_color, utility::toString("poker/bigtag_", 4 - tag, ".png"));
            utility::setSpriteFrame(flag_color_big, utility::toString("poker/bigtag_", 4 - tag, ".png"));
        } else {
            // 红桃 or 方块
            utility::setSpriteFrame(flag_num, utility::toString("poker/red_", num, ".png"));
            utility::setSpriteFrame(flag_color, utility::toString("poker/bigtag_", 4 - tag, ".png"));
            utility::setSpriteFrame(flag_color_big, utility::toString("poker/bigtag_", 4 - tag, ".png"));
        }
    }
    m_data = card;
    this->num->setString(utility::toString(card));
}
